import { combineReducers, configureStore } from "@reduxjs/toolkit";
import { persistStore, persistReducer } from "redux-persist";
import storage from "redux-persist/lib/storage"; // 默认使用 localStorage
import counterStore from "./modules/counterSlice"; // 引入 counterSlice
import listStore from './modules/listSlice'
import userStore from './modules/userInfoSlice'

// 配置持久化
const persistConfig = {
  key: 'root', // 存储的键
  storage, // 使用 localStorage
  // 白名单和黑名单，可以指定哪些 reducer需要持久化
  // whitelist: ['test'], // 只有test会缓存
  // blocklist: ['test'], // 只有test不会缓存
};

// 创建持久化 reducer

// 合并 reducers
// 注册，注意名字要与counterSlice.ts中的name一致
const rootReducer = combineReducers({
  counter: counterStore,
  list: listStore, // 假设 listStore 是你要持久化的另一个 slice
  userStore
});
const persistedReducer = persistReducer(persistConfig, rootReducer);

const store = configureStore({
  reducer: {
    counter: persistedReducer, // 注册持久化的 reducer
  },

  // 关闭redux浏览器存在的警告
    middleware:getDefaultMiddleware => getDefaultMiddleware({
      serializableCheck:false
   })
});

// 创建持久化存储
const persistor = persistStore(store);

// 导出类型
export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;

// 导出 store 和 persistor
export { store, persistor };

